package org.jeecg.modules.wlhy.hytaxpersonalincome.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.third.dongjiang.tax.system.dto.ApiBaseResDTO;
import org.jeecg.modules.third.dongjiang.tax.system.dto.req.personalincome.PersonalincomeDTO;
import org.jeecg.modules.third.dongjiang.tax.system.service.DongjiangTaxSystemService;
import org.jeecg.modules.wlhy.hytaxpersonalincome.entity.HyTaxPersonalincome;
import org.jeecg.modules.wlhy.hytaxpersonalincome.entity.YssrTotalDTO;
import org.jeecg.modules.wlhy.hytaxpersonalincome.mapper.HyTaxPersonalincomeMapper;
import org.jeecg.modules.wlhy.hytaxpersonalincome.service.IHyTaxPersonalincomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 个人所得税详细信息
 * @Author: duanlikao
 * @Date:   2021-03-19
 * @Version: V1.0
 */
@Service
public class HyTaxPersonalincomeServiceImpl extends ServiceImpl<HyTaxPersonalincomeMapper, HyTaxPersonalincome> implements IHyTaxPersonalincomeService {

    @Autowired
    private DongjiangTaxSystemService dongjiangTaxSystemService;

    @Resource
    private  HyTaxPersonalincomeMapper taxPersonalincomeMapper;

    @Override
    public ApiBaseResDTO uploadTax(HyTaxPersonalincome hyTaxPersonalincome) {

        HyTaxPersonalincome hyTaxPersonalincomeDb = getById(hyTaxPersonalincome.getId());
        if(hyTaxPersonalincomeDb == null) {
            throw new JeecgBootException("未找到对应数据");
        }
        //if(hyTaxPersonalincomeDb.getUploadStatus().equals("1")){
        //    throw new JeecgBootException("该条数据已经上报："+hyTaxPersonalincomeDb.getId());
        //}
        PersonalincomeDTO personalincomeDTO = HyTaxPersonalincomeTOPersonalincomeDTO(hyTaxPersonalincomeDb);
        if(personalincomeDTO == null){
            throw new JeecgBootException("未找到对应数据");
        }

        ApiBaseResDTO reportResult = dongjiangTaxSystemService.sendPersonalincome(personalincomeDTO);

        Integer code =  StringUtils.isNotEmpty(reportResult.getCode())? Integer.valueOf(reportResult.getCode()) : 0;

        hyTaxPersonalincomeDb.setUploadStatus(code >= 100 && code < 200 ? "1" :"2");
        hyTaxPersonalincomeDb.setUploadDate(new Date());
        hyTaxPersonalincomeDb.setSbrq(new Date());
        updateById(hyTaxPersonalincomeDb);
        return reportResult;
    }

    /**
     * 统计司机每月已经个税 累计已缴税额
     * @param fwfuuid
     * @param startMonth
     * @param endMonth
     * @return
     */
    @Override
    public YssrTotalDTO getTaxPersonalincomeByFwfuuid(String fwfuuid, String startMonth, String endMonth) {
        return taxPersonalincomeMapper.getTaxPersonalincomeByFwfuuid(fwfuuid,  startMonth,  endMonth);
    }
    /**
     * 按税率统计司机每月已经个税 累计已缴税额
     * @param tjsl
     * @param startMonth
     * @param endMonth
     * @return
     */
    @Override
    public YssrTotalDTO getBqybtseTotalBySl(String tjsl, String startMonth, String endMonth) {
        return taxPersonalincomeMapper.getBqybtseTotalBySl(Double.valueOf(tjsl),  startMonth,  endMonth);
    }

    @Override
    public YssrTotalDTO getNowBqybtseTotalBySl(String tjsl, String startMonth, String endMonth) {
        return taxPersonalincomeMapper.getNowBqybtseTotalBySl(Double.valueOf(tjsl),  startMonth,  endMonth);
    }

    @Override
    public YssrTotalDTO getYssrTotalBySl(String tjsl, String startTime, String endTime) {
        return taxPersonalincomeMapper.getYssrTotalBySl(Double.valueOf(tjsl),  startTime,  endTime);
    }

    public static PersonalincomeDTO HyTaxPersonalincomeTOPersonalincomeDTO(HyTaxPersonalincome hyTaxPersonalincome) {
        if (hyTaxPersonalincome == null) {
            return null;
        }
        PersonalincomeDTO personalincomeDTO = new PersonalincomeDTO();
        personalincomeDTO.setFwfuuid(hyTaxPersonalincome.getFwfuuid());
        personalincomeDTO.setNsrsbh(hyTaxPersonalincome.getNsrsbh());
        personalincomeDTO.setNsrmc(hyTaxPersonalincome.getNsrmc());
        personalincomeDTO.setHy(hyTaxPersonalincome.getHy());
        personalincomeDTO.setXzqh(hyTaxPersonalincome.getXzqh());
        personalincomeDTO.setJdxz(hyTaxPersonalincome.getJdxz());
        personalincomeDTO.setSwjg(hyTaxPersonalincome.getSwjg());
        personalincomeDTO.setSfzjlx(hyTaxPersonalincome.getSfzjlx());
        personalincomeDTO.setSfzjhm(hyTaxPersonalincome.getSfzjhm());
        personalincomeDTO.setXm(hyTaxPersonalincome.getXm());
        personalincomeDTO.setGjdq(hyTaxPersonalincome.getGjdq());
        personalincomeDTO.setLxdh(hyTaxPersonalincome.getLxdh());
        personalincomeDTO.setScjydxzqh(hyTaxPersonalincome.getScjydxzqh());
        personalincomeDTO.setSbrq(
                DateUtils.date2Str( new Date(),DateUtils.date_sdf.get())
        );
        personalincomeDTO.setSkssqq(
                DateUtils.date2Str( hyTaxPersonalincome.getSkssqq(),DateUtils.date_sdf.get())
                );
        personalincomeDTO.setSkssqz(
                DateUtils.date2Str( hyTaxPersonalincome.getSkssqz(),DateUtils.date_sdf.get())
        );
        personalincomeDTO.setYssr(String.format("%.2f", hyTaxPersonalincome.getYssr()));
        personalincomeDTO.setYssdl(String.format("%.2f",hyTaxPersonalincome.getYssdl()));
        personalincomeDTO.setJsyj(String.format("%.2f",hyTaxPersonalincome.getJsyj()));
        personalincomeDTO.setSl(String.format("%.2f",hyTaxPersonalincome.getSl()));
        personalincomeDTO.setSskcs(
                hyTaxPersonalincome.getSskcs() !=null ?  String.format("%.2f",hyTaxPersonalincome.getSskcs()):
                String.valueOf(00.00)
        );
        personalincomeDTO.setYnse(String.format("%.2f",hyTaxPersonalincome.getYnse()));
        personalincomeDTO.setLjyjse(
                hyTaxPersonalincome.getLjyjse() !=null ?  String.format("%.2f",hyTaxPersonalincome.getLjyjse()):
                String.valueOf(00.00)
        );
        personalincomeDTO.setBqybtse(String.format("%.2f",hyTaxPersonalincome.getBqybtse()));

        return personalincomeDTO;
    }

}
